import os
def sort_by_value(d):
    items = d.items()
    backitems = [[v[1] , v[0]] for v in items]
    backitems.sort(reverse=False)
    return [backitems[i][1] for i in range(0,len(backitems))]

files = os.listdir('../result/cookies')
healthydict = set()
fin = open('../ref/DrugAndDisease.dict','r')
for item in fin.readlines():
    healthydict.add(item.strip())

tags = set()
validquerys = set()

sessionnum = 0
for f in files:
    content = open('../result/cookies/'+f).read()
    t = ''
    for w in tags:
        if w in content:
            t = w
            break
    if t=='':
        continue
    sessiondict = dict()
    count = 0
    inittime = 0
    sessiondict[count] = []
    lines = open('../result/cookies/'+f).readlines()
    if len(lines)>500:
        continue
    for l in lines:
        segs = l.strip().split('\t')
        cookie = segs[1]
        time = float(segs[4])
        if time - inittime >1800:
            count += 1
            sessiondict[count] = []
            sessiondict[count].append(l)
        else:
            sessiondict[count].append(l)
        inittime = time
    for index in range(1,len(sessiondict.keys()),1):
        output = True
        valid = False
        key = ''
        content = ''
        for s in sessiondict[index]:
            content += s
        for item in healthydict:
            if item in content:
                key += item
                valid = True
        if key == '':
            key ='EMPTY'
        if valid == True:
            for s in sessiondict[index]:
                validquerys.add(s.strip().split('\t')[5])
        if output == True:
            sessionnum+=1
            timeline = dict()
            for s in sessiondict[index]:
                timeline[s] = float(s.strip().split('\t')[4])
            ses = sort_by_value(timeline)
fout = open('../result/allqueries.dat','w')
for q in validquerys:
    fout.write(q+'\n')
fout.close()